package cn.homework.web.servlet;

import cn.homework.domain.Notice;
import cn.homework.service.NoticeService;
import cn.homework.service.impl.NoticeServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author ACT
 */
@WebServlet("/noticeServlet")
public class NoticeServlet extends HttpServlet {
    private NoticeService service = new NoticeServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String demand = request.getParameter("demand");
        switch (demand) {
            case "show": {
                noticeAllShow(request, response);
                break;
            }
            case "delete": {
                deleteNotice(request, response);
                break;
            }
            case "add": {
                addNotice(request, response);
                break;
            }
            case "query": {
                queryNotice(request, response);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 显示所有公告
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void noticeAllShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Notice> noticeAllList = service.findAllNotice();
        HttpSession session = request.getSession();
        session.setAttribute("noticeAllList", noticeAllList);
        response.sendRedirect(request.getContextPath() + "/BackstageNoticeShow.jsp");
    }

    /**
     * 根据id删除公告
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void deleteNotice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        service.deleteNoticeById(id);
        noticeAllShow(request, response);
    }

    /**
     * 添加公告
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void addNotice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String message = request.getParameter("message");
        String admin = request.getParameter("admin");
        service.addNotice(message, admin);
        noticeAllShow(request, response);
    }

    /**
     * 根据条件查询公告
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void queryNotice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        List<Notice> noticeList = service.findNoticeByCondition(map);
        HttpSession session = request.getSession();
        session.setAttribute("noticeList", noticeList);
        response.sendRedirect(request.getContextPath() + "/BackstageNoticeQuery.jsp");
    }
}
